package com.xunqi.permission.controller;

import com.xunqi.permission.common.JsonData;
import com.xunqi.permission.dto.AclModuleLevelDto;
import com.xunqi.permission.param.AclModuleParam;
import com.xunqi.permission.service.SysAclModuleService;
import com.xunqi.permission.service.SysTreeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


@Api(tags = "角色权限控制器")
@Controller
@RequestMapping(value = "/sys/aclModule")
@Slf4j
public class SysAclModuleController {

    @Resource
    private SysAclModuleService sysAclModuleService;
    @Resource
    private SysTreeService sysTreeService;

    @ApiOperation(value = "跳转到权限控制界面",httpMethod = "GET",protocols = "HTTP",produces = "/acl.page",response = String.class)
    @RequestMapping("/acl.page")
    public String acl() {
        return "acl";
    }

    @ApiOperation(value = "权限新增功能",httpMethod = "POST",protocols = "HTTP",produces = "/save.json",response = JsonData.class)
    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData saveAclModule(AclModuleParam param) {
        sysAclModuleService.save(param);
        return JsonData.success();
    }

    @ApiOperation(value = "权限更新功能",httpMethod = "POST",protocols = "HTTP",produces = "/update.json",response = JsonData.class)
    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData updateAclModule(AclModuleParam param) {
        sysAclModuleService.update(param);
        return JsonData.success();
    }

    @ApiOperation(value = "权限树状形页面的显示",httpMethod = "GET",protocols = "HTTP",produces = "/tree.json",response = JsonData.class)
    @RequestMapping("/tree.json")
    @ResponseBody
    public JsonData tree() {
        List<AclModuleLevelDto> dtoList = sysTreeService.aclModuleTree();
        return JsonData.success(dtoList);
    }


    @ApiOperation(value = "角色权限的删除功能",httpMethod = "GET",protocols = "HTTP",produces = "/delete.json",response = JsonData.class,
            notes = "先判断该模块下面是否有子模块，再判断子模块下面是否有用户存在，若有则提示用户删除失败，若没有，则删除成功!")
    @RequestMapping(value = "/delete.json")
    @ResponseBody
    public JsonData delete(@RequestParam("id") int id) {
        sysAclModuleService.delete(id);
        return JsonData.success();
    }

}
